package it.polimi.meteocal_pappalardo_sacchi.bean;

import it.polimi.meteocal_pappalardo_sacchi.entity.BadWeatherNotificationWithSuggestion;
import it.polimi.meteocal_pappalardo_sacchi.entity.Event;
import it.polimi.meteocal_pappalardo_sacchi.entity.Notification;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 *
 */
@Named
@ViewScoped
public class NotificationBean implements Serializable {

    @Inject
    private NotificationManager notificationManager;
    @Inject
    private UserBean userBean;
    @Inject
    private EventManager eventManager;

    List<Notification> unreadNotificationByUser;
    List<Notification> readNotificationByUser;

    @PostConstruct
    private void init() {
        unreadNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), false);
        readNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), true);
    }

    public List<Notification> getUnreadNotificationByUser() {
        return unreadNotificationByUser;
    }

    public List<Notification> getReadNotificationByUser() {
        return readNotificationByUser;
    }

    public void selectAllUnread(boolean orUnselect) {
        if (orUnselect) {
            for (Notification n: getUnreadNotificationByUser()) {
                n.setRead(false);
            }
        } else {
            for (Notification n: getUnreadNotificationByUser()) {
                n.setRead(true);
            }
        }
    }

    public void showEventModificationNotification(boolean isRead) {
        if (!isRead) {
            unreadNotificationByUser = notificationManager.findEventModificationNotificationByUser(userBean.getUser(), false);
        } else {
            readNotificationByUser = notificationManager.findEventModificationNotificationByUser(userBean.getUser(), true);
        }
    }

    public void showInvitation(boolean isRead) {
        if (!isRead) {
            unreadNotificationByUser = notificationManager.findInvitationByUser(userBean.getUser(), false);
        } else {
            readNotificationByUser = notificationManager.findInvitationByUser(userBean.getUser(), true);
        }
    }

    public void showBadWeatherNotification(boolean isRead) {
        if (!isRead) {
            unreadNotificationByUser = notificationManager.findBadWeatherNotificationByUser(userBean.getUser(), false);
        } else {
            readNotificationByUser = notificationManager.findBadWeatherNotificationByUser(userBean.getUser(), true);
        }    }

    public void showAll(boolean isRead) {
        if (!isRead) {
            unreadNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), false);
        } else {
            readNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), true);
        }
    }

    public void updateUnreadNotification(boolean orViceversa) {
        if (!orViceversa) {
            for (Notification n: getUnreadNotificationByUser()) {
                notificationManager.edit(n);
            }
        } else {
            for (Notification n: getReadNotificationByUser()) {
                notificationManager.edit(n);
            }
        }
        unreadNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), false);
        readNotificationByUser = notificationManager.findNotificationByUser(userBean.getUser(), true);
    }
    
    public void accept(BadWeatherNotificationWithSuggestion notification){       
        Event event = notification.getRelatedEvent();
        event.setStartingDate(notification.getNewStartingDate());
        event.setEndingDate(notification.getNewEndingDate());
        eventManager.edit(event);
        notification.setNewStartingDate(null);
        notification.setNewEndingDate(null);        
        notificationManager.edit(notification);
        //passo null come lista degli utente da notificare il mondo che sia il metodo a ricavarla dal db
        notificationManager.sendModificationNotification(null, event, event, "The starting and ending date of event "
                + event.getTitle() + " has changed. New starting date: " + event.getStartingDate().toString()
                + " new ending date: " + event.getEndingDate().toString());
    }
}
